<?php
namespace backend\components;

use yii;
use yii\helpers\Url;

/**
 * swfupload 插件封装
 */
class swfupload
{
    /**
     * 初始化 SWFupload 插件
     * 配置swfupload上传中需要的参数
     * @param $module 模块名称
	 * @param $catid 栏目id
	 * @param $args 传递参数
	 * @param $userid 用户id
     */
    public static function initswfupload($module,$catid=0,$args,$userid)
    {
        extract(self::getswfinit($args));
        $init = 'var swfu = \'\';
		$(document).ready(function(){
		swfu = new SWFUpload({
			flash_url:"'.Yii::$app->request->hostInfo.'/swfupload.swf",
			upload_url:"'.Yii::$app->request->hostInfo.'/backend/web/content/attachment/upload",
			file_post_name : "upfile",
			post_params:{"'.Yii::$app->request->csrfParam.'":"'.Yii::$app->request->getCsrfToken().'","catid":"'.$catid.'","module":"'.$module.'","userid":"'.$userid.'"},
			file_size_limit:"2 MB",
			file_types:"'.$file_types.'",
			file_types_description:"All Files",
			file_upload_limit:"'.$file_upload_limit.'",
			custom_settings : {progressTarget : "fsUploadProgress",cancelButtonId : "btnCancel"},
	 
			button_image_url: "",
			button_width: 75,
			button_height: 28,
			button_placeholder_id: "buttonPlaceHolder",
			button_text_top_padding: 3,
			button_text_left_padding: 12,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,

			file_dialog_start_handler : fileDialogStart,
			file_queued_handler : fileQueued,
			file_queue_error_handler:fileQueueError,
			file_dialog_complete_handler:fileDialogComplete,
			upload_progress_handler:uploadProgress,
			upload_error_handler:uploadError,
			upload_success_handler:uploadSuccess,
			upload_complete_handler:uploadComplete
			});
        })';
        return $init;
    }

    /**
     * 读取swfupload配置类型
     * @param array $args flash上传配置信息
     */
    public static function getswfinit($args)
    {
        $site_allowext = 'jpg|jpeg|gif|bmp|png|doc|docx|xls|xlsx|ppt|pptx|pdf|txt|rar|zip|swf';
        $args = explode(',',$args);
        $arr['file_upload_limit'] = intval($args[0]) ? intval($args[0]) : '1';
        $args['1'] = ($args[1]!='') ? $args[1] : $site_allowext;
        $arr_allowext = explode('|',$args[1]);
        foreach($arr_allowext as $k=>$v)
        {
            $v = '*.'.$v;
            $array[$k] = $v;
        }
        $upload_allowext = implode(';' , $array);
        $arr['file_types'] = $upload_allowext;
		$arr['file_types_post'] = $args[1];
		$arr['allowupload'] = intval($args[2]);
		$arr['thumb_width'] = intval($args[3]);
		$arr['thumb_height'] = intval($args[4]);
		$arr['watermark_enable'] = ($args[5]=='') ? 1 : intval($args[5]);
		return $arr;
	}
	
	/**
	 * 返回附件类型图标
	 * @param $file 附件名称
	 * @param $type png为大图标，gif为小图标
	 */
	public static function file_icon($file,$type = 'png') {
		$ext_arr = array('doc','docx','ppt','xls','txt','pdf','mdb','jpg','gif','png','bmp','jpeg','rar','zip','swf','flv');
		$ext = self::fileext($file);
		if($type == 'png') {
			if($ext == 'zip' || $ext == 'rar') $ext = 'rar';
			elseif($ext == 'doc' || $ext == 'docx') $ext = 'doc';
			elseif($ext == 'xls' || $ext == 'xlsx') $ext = 'xls';
			elseif($ext == 'ppt' || $ext == 'pptx') $ext = 'ppt';
			elseif ($ext == 'flv' || $ext == 'swf' || $ext == 'rm' || $ext == 'rmvb') $ext = 'flv';
			else $ext = 'do';
		}
		if(in_array($ext,$ext_arr)) return Url::to('@web/images/ext/'.$ext.'.'.$type, true);
		else return Url::to('@web/images/ext/blank.'.$type, true);
	}

	/**
     * 取得文件扩展
     *
     * @param $filename 文件名
     * @return 扩展名
     */
    public static function fileext($filename) {
        return strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
    }

}











?>